package javaTodotxt;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;

public class TodotxtFileIO {
	
	private String todoFile;
	private String todoName;
	private String context;
	
	public TodotxtFileIO(){
		todoFile = "";
		todoName = "";
		context = "";
	}
	
	public void setTodoFile(String todoFile){
		this.todoFile = todoFile;
	}
	
	public void SetTodoName(String todoName){
		this.todoName = todoName;
	}
	
	public void setContext(String context){
		this.context = context;
	}
	
	public String getTodoFile(){
		return todoFile;
	}
	
	public String getTodoName(){
		return todoName;
	}
	
	public String getContext(){
		return context;
	}
	
	
	
	public TodotxtFileIO(String todoFile, String todoName, String context){
		this.todoFile = todoFile;
		this.todoName = todoName;
		this.context = context;
	}
	

	public String readLine(int row) throws IOException {
        BufferedReader inputStream = null;
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            
            String l = "";
            int i = 0;
            while (l != null && i < row) {
            	i++;
            	l = inputStream.readLine();
            }
            if (l == null){
            	return "Task not found.";
            }else{
            	return l;
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
	
	public ArrayList<String> read() throws IOException {
		BufferedReader inputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
        	inputStream = new BufferedReader(new FileReader(todoFile));
        	
            String l;
            int i = 0;
            while ((l = inputStream.readLine()) != null) {
            	i++;
            	l = Integer.toString(i)+" "+l;
            	tasks.add(l);
            }
            return tasks;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
	}
	
	public ArrayList<String> readPriority() throws IOException {
		BufferedReader inputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
		ArrayList<String> tasksSorted = new ArrayList<String>();
		ArrayList<Integer> lines = new ArrayList<Integer>();
        
        try {
        	inputStream = new BufferedReader(new FileReader(todoFile));
        	
            
            String l;
            int i = 0;
            while ((l = inputStream.readLine()) != null) {
            	i++;
            	lines.add(i);
            	if (l.length() > 0){
	            	if(l.substring(0, 1).equals("(")){
	            		tasks.add(l);
	            	}
            	}
            }
            Collections.sort(tasks);
            for (i = 0; i < tasks.size(); i++){
            	tasksSorted.add(lines.get(i).toString()+" "+tasks.get(i));
            }
            return tasksSorted;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
	}
	
	public ArrayList<String> search(ArrayList<String> srch) throws IOException {
		BufferedReader inputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
        	inputStream = new BufferedReader(new FileReader(todoFile));
        	
            String l;
            int i = 0;
            while ((l = inputStream.readLine()) != null) {
            	boolean match = true;
            	i++;
            	for (int j = 0; j < srch.size(); j++){
            		String srchTerm = "";
            		if (srch.get(j).substring(0, 1).equals("-")){
            			srchTerm = srch.get(j).substring(1);
            			if (l.toLowerCase().contains(srchTerm.toLowerCase())){
                			match = false;
                		}
            		}else{
            			srchTerm = srch.get(j);
            			if (!l.toLowerCase().contains(srchTerm.toLowerCase())){
                			match = false;
                		}
            		}            		
            	}
            	if (match){
            		l = Integer.toString(i)+" "+l;
            		tasks.add(l);
            	}
            }
            
            return tasks;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
	}
	
	public ArrayList<String> searchPriority(ArrayList<String> srch) throws IOException {
		BufferedReader inputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
		ArrayList<String> tasksSorted = new ArrayList<String>();
		ArrayList<Integer> lines = new ArrayList<Integer>();
         
        try {
        	inputStream = new BufferedReader(new FileReader(todoFile));
        	
            String l;
            int i = 0;
            while ((l = inputStream.readLine()) != null) {
            	boolean match = true;
            	for (int j = 0; j < srch.size(); j++){
            		String srchTerm = "";
            		if (srch.get(j).substring(0, 1).equals("-")){
            			srchTerm = srch.get(j).substring(1);
            			if (l.toLowerCase().contains(srchTerm.toLowerCase())){
                			match = false;
                		}
            		}else{
            			srchTerm = srch.get(j);
            			if (!l.toLowerCase().contains(srchTerm.toLowerCase())){
                			match = false;
                		}
            		}
            	}
            	if (match && l.substring(0, 1).equals("(")){
            		i++;
                	lines.add(i);
                	tasks.add(l);
            	}
            }
            Collections.sort(tasks);
            for (i=0;i<tasks.size();i++){
            	tasksSorted.add(lines.get(i).toString()+" "+tasks.get(i).split(":")[0]);
            }
            return tasksSorted;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
	}
	
	public void add(String task) throws IOException {
		PrintWriter outputStream = null;
		
		 try {
	            outputStream = 
	                new PrintWriter(new FileWriter(todoFile,true));

	            outputStream.println(task);
	            
	        } finally {
	            if (outputStream != null) {
	                outputStream.close();
	            }
	        }
	}
	
	public void addMultiple(ArrayList<String> tasks) throws IOException {
		PrintWriter outputStream = null;
		
		 try {
	            outputStream = 
	                new PrintWriter(new FileWriter(todoFile,true));

	            for (int i = 0; i < tasks.size(); i++){
	            	outputStream.println(tasks.get(i));
	            }
	            
	        } finally {
	            if (outputStream != null) {
	                outputStream.close();
	            }
	        }
	}
	
	public void insert(String task, int row) throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            int i =0;
            String l;
            while ((l = inputStream.readLine()) != null) {
            	i ++;
            	if (i == row){
            		tasks.add(task);
            	}
            	tasks.add(l);
            }
            outputStream = 
                new PrintWriter(new FileWriter(todoFile));
            i = 0;
            while (i < tasks.size()){
            	outputStream.println(tasks.get(i));
            	i++;
            }

            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
	}
	
	public void replace(String task, int row) throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            int i =0;
            String l;
            while ((l = inputStream.readLine()) != null) {
            	i ++;
            	if (i == row){
            		tasks.add(task);
            	}else{
            		tasks.add(l);
            	}
            }
            outputStream = 
                new PrintWriter(new FileWriter(todoFile));
            i = 0;
            while (i < tasks.size()){
            	outputStream.println(tasks.get(i));
            	i++;
            }

            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
	}
	
	public void append(String text, int row) throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            int i =0;
            String l;
            while ((l = inputStream.readLine()) != null) {
            	i ++;
            	if (i == row){
            		tasks.add(l+text);
            	}else{
            		tasks.add(l);
            	}
            }
            outputStream = 
                new PrintWriter(new FileWriter(todoFile));
            i = 0;
            while (i < tasks.size()){
            	outputStream.println(tasks.get(i));
            	i++;
            }

            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
	}
	
	public void prepend(String text, int row) throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            int i =0;
            String l;
            while ((l = inputStream.readLine()) != null) {
            	i ++;
            	String pri = "";
            	if (i == row){
            		if(l.substring(0, 1).equals("(")){
            			pri = l.substring(0, 4);
            			l = l.substring(4);
            		}
            		tasks.add(pri+text+l);
            	}else{
            		tasks.add(l);
            	}
            }
            outputStream = 
                new PrintWriter(new FileWriter(todoFile));
            i = 0;
            while (i < tasks.size()){
            	outputStream.println(tasks.get(i));
            	i++;
            }

            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
	}
	
	public ArrayList<String> archive() throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputTodo = null;
		ArrayList<String> tasks = new ArrayList<String>();
		ArrayList<String> arch = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            String l;
            while ((l = inputStream.readLine()) != null) {
            	if (l.substring(0, 2).equals("X ")){
            		arch.add(l.substring(2));
            	}else{
            		tasks.add(l);
            	}
            }
            inputStream.close();
            outputTodo = new PrintWriter(new FileWriter(todoFile));
            for (int i = 0;i < tasks.size();i++){
            	outputTodo.println(tasks.get(i));
            }
            outputTodo.close();
            
            return arch;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputTodo != null) {
            	outputTodo.close();
            }
        }
	}
	
	public void remove(int row) throws IOException {
		BufferedReader inputStream = null;
		PrintWriter outputStream = null;
		ArrayList<String> tasks = new ArrayList<String>();
        
        try {
            inputStream = 
                new BufferedReader(new FileReader(todoFile));
            int i =0;
            String l;
            while ((l = inputStream.readLine()) != null) {
            	i ++;
            	if (i != row){
            		tasks.add(l);
            	}
            }
            outputStream = 
                new PrintWriter(new FileWriter(todoFile));
            i = 0;
            while (i < tasks.size()){
            	outputStream.println(tasks.get(i));
            	i++;
            }

            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
	}
	
	
}
